package x.y.z;

import java.util.HashMap;
import java.util.Map;

public class LeetCode_0003 {
    public int lengthOfLongestSubstring(String s) {
        if (s.isEmpty()) {
            return 0;
        }
        int left = 0;
        int right = 0;
        int res = Integer.MIN_VALUE;
        Map<Character, Integer> map = new HashMap<>();
        while (right < s.length()) {
            char ch = s.charAt(right);
            map.put(ch, map.getOrDefault(ch, 0) + 1);
            right++;

            while (map.get(ch) > 1) {
                char c = s.charAt(left);
                map.put(c, map.get(c) - 1);
                left++;
            }
            res = Math.max(res, right - left);
        }
        return res;
    }
}
